﻿using System;
using System.IO;
using PBLOG.Interfaces;
using PBLOG.Collections;
using PBLOG.Helpers;

namespace PBLOG
{
    public sealed class Logger : ILogger
    {
        private string _filename = String.Empty;
        private bool _userOwnerLogs = false;
        private string _appName = String.Empty;

        private string DefaultPath()
        {
            return Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                                                 AppDomain.CurrentDomain.FriendlyName
                                                                                .Replace(".exe", "")
                                                                                .Replace(".vshost", "") + ".xml");
        }

        /// <summary>
        /// Initialize the XML logger with default parameters
        /// </summary>
        public Logger()
        {
            this._filename = this.DefaultPath();

            XmlHelper.SetPath(this._filename);
        }

        /// <summary>
        /// Specific a filename of XML logger
        /// </summary>
        /// <param name="filename">Filename XML logger</param>
        public Logger(string filename)
        {
            string checkedPath = Utilities.CheckedPathXml(filename);

            this._filename = checkedPath;

            XmlHelper.SetPath(checkedPath);
        }

        /// <summary>
        /// Users are owners of logs (if you delete a user, logs of that user, will be deleted)
        /// </summary>
        /// <param name="userOwnerLogs">Default is false (to delete only the user)</param>
        public Logger(bool userOwnerLogs)
        {
            this._filename = this.DefaultPath();

            XmlHelper.SetPath(this._filename);

            this._userOwnerLogs = userOwnerLogs;

            XmlHelper.UserOwnerLogs = userOwnerLogs;
        }

        /// <summary>
        /// Specific a filename of XML logger and if users are owners of logs
        /// </summary>
        /// <param name="filename">Filename XML logger</param>
        /// <param name="userOwnerLogs">Default is false (to delete only the user)</param>
        public Logger(string filename, bool userOwnerLogs)
        {
            string checkedPath = Utilities.CheckedPathXml(filename);

            this._filename = checkedPath;

            XmlHelper.SetPath(checkedPath);

            this._userOwnerLogs = userOwnerLogs;

            XmlHelper.UserOwnerLogs = userOwnerLogs;
        }

        /// <summary>
        /// Gets filename of XML logger
        /// </summary>
        public string Filename
        {
            get { return this._filename; }
        }

        /// <summary>
        /// Gets or sets name of applicazione (default is FriendlyName)
        /// </summary>
        public string ApplicationName
        {
            get { return this._appName; }
            set { this._appName = value; }
        }

        /// <summary>
        /// Gets or sets if users are owners of logs
        /// </summary>
        public bool UserOwnerLogs
        {
            get { return this._userOwnerLogs; }
            set { this._userOwnerLogs = value; }
        }

        /// <summary>
        /// Gets all users
        /// </summary>
        public UsersCollection Users { get { return new UsersCollection(); } }

        /// <summary>
        /// Gets all kinds of logs
        /// </summary>
        public LogsCollection Logs { get { return new LogsCollection(); } }
    }
}